<template>
	<ClassifyFiltrate v-if="show" only :criteriaList="criteriaList" @change="conditionChange" />
</template>

<script lang="ts" setup name="WritTemplate">
	import ClassifyFiltrate from '@/components/classify-filtrate/classify-filtrate.vue';
	import type { Criteria } from '@/types/filtrate-condition';
	import { ServiceType } from '@/utils/constant';
	import { ref, onBeforeMount, defineProps, withDefaults, watchEffect } from 'vue';
	import { getServiceCategoryList, getServiceList } from '@/apis/service';
	import type { AnyObj } from '@/types/global';
	const show = ref(false);
	const props = withDefaults(defineProps<{
		parentId : number;
	}>(), {
		parentId: 1
	});
	const criteriaList = ref<Criteria[]>([
		// {
		// 	id: 'unlimited',
		// 	text: '不限',
		// 	selected: false,
		// 	field: 'unlimited',
		// 	type: 'radio',
		// 	dataType: 'text',
		// 	value: '',
		// 	hidden: true,
		// 	options: [
		// 	{ text: '公司公司法律文书模版', value: '1001' }
		// ]
		// }
	]);

	function conditionChange(query : Record<string, any>, option : AnyObj) {
		console.log('查询条件', query, option);
		uni.navigateTo({
			url: `/pages-service/service-detail/service-detail?spuId=${option.spuId}`
		})
	}

	function loadData() {
		show.value = false;
		Promise.all([getServiceCategoryList(props.parentId), getServiceList(props.parentId)]).then((posts) => {
			const [categoryResult, serviceResult] = posts;
			const categoryMap : AnyObj = {};
			const categoryList = [
				// {
				// 	id: 'unlimited',
				// 	text: '不限',
				// 	selected: false,
				// 	field: 'unlimited',
				// 	type: 'radio',
				// 	dataType: 'text',
				// 	value: '',
				// 	hidden: true,
				// 	options: []
				// }
			]
			for (const item of categoryResult.rows) {
				const category = {
					id: `categoryId_${item.categoryId}`,
					text: item.categoryName,
					selected: false,
					field: `field_${item.categoryId}`,
					type: 'radio',
					dataType: 'text',
					value: '',
					hidden: true,
					options: []
				};
				categoryMap[category.id] = category;
				categoryList.push(category);
			}


			for (const item of serviceResult.rows) {
				const fi = categoryMap[`categoryId_${item.categoryId}`];
				if (fi) {
					fi.options.push({ text: item.spuName, value: item.spuId, ...item, imageUrl: item.mainImgUrl })
				}
			}


			criteriaList.value = categoryList as any;
			show.value = true;
			console.log(categoryResult.rows);
			console.log(serviceResult.rows);
		});
	}

	watchEffect(() => {
		loadData();
	})

	onBeforeMount(() => {
	})
</script>

<style lang="scss">
</style>